<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  add_crumb(t('#crumbs.people', "People"), context_url(@context, :context_users_url))
  add_crumb(context_user_name(@context, @user), context_url(@context, :context_user_url, @user))
  @active_tab="people"
%>
<% content_for :page_title do %><%= join_title context_user_name(@context, @user), @context.name %><% end %>

<% content_for :right_side do %>
  <div id="right_nav">
    <% if can_do(@membership, @current_user, :read_grades) && @membership.is_a?(StudentEnrollment) %>
      <a class="btn button-sidebar-wide" href="<%= context_url(@context, :controller => :gradebooks, :action => :grade_summary, :id => @user.id) %>">
        <i class="icon-check-plus" aria-hidden="true"></i>
        <%= t "Student Grades" %>
      </a>
    <% end %>
    <% if @domain_root_account.enable_user_notes and can_do(@user, @current_user, :read_user_notes) %>
      <a href="<%= user_user_notes_path(@user) %>" class="btn button-sidebar-wide">
        <i class="icon-note-light" aria-hidden="true"></i>
        <%= t "Faculty Journal" %>
      </a>
    <% end %>
    <% if @context.is_a?(Course)%>
      <% if @context.user_is_instructor?(@user) && can_do(@user, @current_user, :read_reports) %>
        <a href="<%= user_course_teacher_activity_url(@user, @context) %>" class="btn button-sidebar-wide">
          <i class="icon-chat" aria-hidden="true"></i>
          <%= t 'Student Interactions Report' %>
        </a>
      <% elsif @context.user_has_been_student?(@user) && @context.user_is_instructor?(@current_user) && can_do(@context, @current_user, :read_reports) %>
        <a href="<%= user_student_teacher_activity_url(@current_user, @user) %>" class="btn button-sidebar-wide">
          <i class="icon-chat" aria-hidden="true"></i>
          <%= t 'Interactions Report' %>
        </a>
      <% end %>
    <% end %>
    <% if can_do(@context, @current_user, :read_reports) %>
      <a href="<%= context_url(@context, :context_user_usage_url, @user) %>" class="btn button-sidebar-wide">
        <i class="icon-clock" aria-hidden="true"></i>
        <%= t "Access Report" %>
      </a>
    <% end %>
    <% if @context.is_a?(Course) && @context.has_outcomes? && can_do(@context, @current_user, :manage) %>
      <a href="<%= context_url(@context, :context_user_outcomes_results_url, @user.id) %>" class="btn button-sidebar-wide">
        <i class="icon-outcomes" aria-hidden="true"></i>
        <%= t "Outcomes Report" %>
      </a>
    <% end %>
    <% if @user.grants_right?(@current_user, :view_statistics) %>
      <a href="<%= user_path(@user.id) %>" class="btn button-sidebar-wide">
        <i class="icon-user" aria-hidden="true"></i>
        {{#t}}User Account Details{{/t}}
      </a>
    <% end %>
    <% roster_user_custom_links(@user).each do |link| %>
      <a href="<%= link[:url] %>" class="btn button-sidebar-wide">
        <i class="<%= link[:icon_class] %>" aria-hidden="true"></i>
        <%= link[:text] %>
      </a>
    <% end %>
  </div>
  <a href="<%= user_profile_path(@current_user) %>" class="profile_url" style="display: none;">&nbsp;</a>
<% end %>

<% js_bundle :user_name %>
<% css_bundle :roster_user %>

{{> views/shared/profile}}

<% can_manage_students = can_do(@context, @current_user, :manage_students) %>
<% can_manage_admins = can_do(@context, @current_user, :manage_admin_users) %>

<% if !@context.is_a?(Group) && (can_manage_students || can_manage_admins) %>
<div class="more_user_information">
  <% if @context.is_a?(Course) %>
    <fieldset>
      <legend>
        <h4 class="profileHeader">
          {{#t}}Membership(s){{/t}}
        </h4>
      </legend>
      <table class="ic-Table--condensed">
        <% @enrollments.sort_by(&:id).each do |enrollment| %>
          <tr id="enrollment">
            <td scope="row" style="width: 30%;">
              <a href="<%= context_url(@context, :context_section_url, enrollment.course_section_id) rescue "#" %>"><%= enrollment.try_rescue(:course_section).try_rescue(:display_name) || enrollment.short_name %></a>
            </td>
            <td style="width: 30%;">
              <%= t 'enrolled_as', "Enrolled as a %{enrollment_type}", :enrollment_type => enrollment.readable_type %> | created <%= datetime_string(enrollment.created_at) %>
            </td>
            <% if enrollment.is_a?(ObserverEnrollment) %>
              <td style="<%= hidden unless enrollment.associated_user %>" class="associated_user" >
                <%= t 'enrollment_linked_to', "linked to *%{linked_user}*",
                    :linked_user => context_user_name(@context, enrollment.associated_user),
                    :wrapper => '<span class="associated_user_name">\1</span>' %>
              </td>
            <% end %>
            <td style="width: 20%; <%= hidden unless enrollment.completed? %>">
              <p class="completed_at_holder"><%= t 'completed_enrollment', "completed *%{completed_at}*", :completed_at => datetime_string(enrollment.completed_at), :wrapper => '<span class="completed_at">\1</span>' %></p>
            </td>
            <td style="width: 10%;">
            <% if enrollment.admin? ? (can_manage_admins && enrollment.user_id != @current_user.id) : can_manage_students %>
              <% unless @context.completed? %>
                <p class="conclude_enrollment_link_holder" style="<%= hidden if enrollment.completed? %>">
                  <a class="conclude_enrollment_link" href="<%= context_url(@context, :controller => :courses, :action => :conclude_user, :id => enrollment.id) %>">{{#t}}Conclude{{/t}}</a>
                </p>
                <p class="unconclude_enrollment_link_holder" style="<%= hidden unless enrollment.explicitly_completed? || enrollment.inactive? %>">
                  <a class="unconclude_enrollment_link" href="<%= context_url(@context, :controller => :courses, :action => :unconclude_user, :id => enrollment.id) %>">{{#t}}Restore{{/t}}</a>
                </p>
              <% end %>
            </td>
            <td style="width: 10%;">
              <p>
                <% if enrollment.defined_by_sis? %>
                  <a href="#" title="{{#t}}Can't Delete{{/t}}</a>
                <% else %>
                  <a class="delete_enrollment_link" href="<%= context_url(@context, :context_unenroll_url, enrollment.id) %>">{{#t}}Delete{{/t}}</a>
                <% end %>
              </p>
            </td>
            <% end %>
          </tr>
        <% end %>
        <% if @context.is_a?(Course) && can_do(@context, @current_user, :manage_admin_users) %>
          <tr id="priveleges">
            <td scope="row">
              <%= before_label('user_privileges', %{Privileges}) %>
            </td>
            <td colspan="2">
              <p class="elevate_enrollment_link_holder" style="<%= hidden if @enrollments.any?{|e| !e.limit_privileges_to_course_section } %>">
                {{#t}}this user can only view students in their assigned course section(s){{/t}}<br/>
                <a href="<%= context_url(@context, :context_limit_user_grading_url, @user.id) %>" class="elevate_enrollment_link">{{#t}}let this user see all course users{{/t}}</a>
              </p>
              <p class="restrict_enrollment_link_holder" style="<%= hidden unless @enrollments.any?{|e| !e.limit_privileges_to_course_section } %>">
                {{#t}}this user can view students in any course section{{/t}}<br/>
                <a href="<%= context_url(@context, :context_limit_user_grading_url, @user.id) %>" class="restrict_enrollment_link">{{#t}}limit this user to only see fellow section users{{/t}}</a>
              </p>
            </td>
          </tr>
        <% end %>
      </table>
    </fieldset>
    {{> views/courses/link_enrollment}}
  <% end %>
  {{> views/users/logins}}
</div>
<% end %>

<% js_bundle 'legacy/context_roster_user' %>
